import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class GatesOfAndaron_TMH(Model3D):
    '''Replace 'GatesOfAndaron_TMH' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(GatesOfAndaron_TMH,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.meshType = -1
        
    def parse_vertices(self, meshName, numVerts, meshType):
        
        if meshType == 0:
            for i in range(numVerts):
                vx, vy, vz = self.inFile.read_float(3)
                nx, ny, nz, tu, tv, unk1, unk2 = self.inFile.read_float(7)
                
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
        else:
            for i in range(numVerts):
                vx, vy, vz = self.inFile.read_float(3)
                self.inFile.read_float(9)
                
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
            
    def parse_faces(self, meshName, numFaces):
        
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            
    def parse_face_section(self, meshName):
        
        numSects = self.inFile.read_long()
        print numSects
        for i in range(numSects):
            numIdx = self.inFile.read_long()
            unk = self.inFile.read_short()
            #print numIdx, unk, self.inFile.tell()
            self.parse_faces(meshName, numIdx / 3)
        print self.inFile.tell()
        
    def parse_file(self):
        
        meshNum = 0
        #self.filesize = self.inFile.filesize()
        #while self.inFile.tell() != self.filesize:
        meshName = "obj%d" %meshNum
        self.create_object(meshName)
        
        self.inFile.read_long()
        meshType = self.inFile.read_long()
        self.inFile.read_long()
        self.inFile.read_byte()
        self.inFile.read_float(meshType*16)
        self.inFile.read_float(4)
        numVerts = self.inFile.read_long()
        self.parse_vertices(meshName, numVerts, meshType)
        self.parse_face_section(meshName)
        self.parse_face_section(meshName)
        self.parse_face_section(meshName)
        self.parse_face_section(meshName)
        #print "EOF"
        
        
def read_file(path):
    '''Read the file'''
    
    
    
    openFile = StructReader(open(path, 'rb'))
    obj = GatesOfAndaron_TMH(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "TMH", "Gates of Andaron TMH"